<?php

/**
 * 获取用户的真实ip,仅适用于cgi运行方式，cli运行方式请不要使用
 *
 * @return array|false|mixed|string
 */
function GetRealIp()
{
    static $_map = [];
    if (isset($_map['ip'])) {
        $ip = $_map['ip'];
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
            $ip = getenv("HTTP_X_FORWARDED_FOR");
            $ip = explode(',', $ip);
            $len = count($ip);
            $tmp = trim($_SERVER['REMOTE_ADDR']);
            for ($i = 0; $i < $len; $i++) {
                $t = trim($ip[$i]);
                if ($t != "127.0.0.1") {
                    $tmp = $t;
                }
            }
            $ip = $tmp;
        } else {
            if (isset($_SERVER["HTTP_FW_ADDR"])) {
                $ip = trim($_SERVER["HTTP_FW_ADDR"]);
            } else {
                if (isset($_SERVER["HTTP_CDN_SRC_IP"])) {
                    $ip = trim($_SERVER["HTTP_CDN_SRC_IP"]);
                } else {
                    if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown") && CheckIP(getenv("REMOTE_ADDR"))) {
                        $ip = trim(getenv("REMOTE_ADDR"));
                    } else {
                        if (isset ($_SERVER ['REMOTE_ADDR']) && $_SERVER ['REMOTE_ADDR'] && strcasecmp($_SERVER ['REMOTE_ADDR'], "unknown") && CheckIP($_SERVER ["REMOTE_ADDR"])) {
                            $ip = trim($_SERVER ['REMOTE_ADDR']);
                        } else {
                            if (isset($_SERVER["HTTP_X_CONNECTING_IP"])) {
                                $ip = trim($_SERVER["HTTP_X_CONNECTING_IP"]);
                            } else {
                                $ip = "127.0.0.1";
                            }
                        }
                    }
                }
            }
        }

        $_map['ip'] = $ip;
    }
    $regip = '/^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$/';
    if (!preg_match($regip, $ip)) {
        exit();
    }

    return $ip;
}

/**
 * 是否为内部ip
 *
 * @param $ip
 *
 * @return bool
 */
function CheckIP($ip)
{
    if (preg_match("/^(192\.168|127\.0\.0)\./", $ip) && getenv("HTTP_X_FORWARDED_FOR")) {
        return false;
    } else {
        return true;
    }
}

/**
 * @param int $ip
 *
 * @return int
 */
function getIp2long($ip = 0)
{
    if (empty($ip)) {
        $ip = GetRealIp();
    }
    $ip_arr = explode('.', $ip);
    $iplong = (16777216 * intval($ip_arr[0])) + (65536 * intval($ip_arr[1])) + (256 * intval($ip_arr[2])) + intval($ip_arr[3]);

    return $iplong;
}


/**
 * @desc dump json
 *
 * @param array $data
 */
if (!function_exists('dumpJson')) {

    function dumpJson($data = [])
    {
        header('Content-Type:application/json; charset=utf-8');
        echo json_encode($data);
    }
}


/**
 * @desc CRC32的修正方法，修正php x86模式下出现的负值情况
 *
 * @param $str
 *
 * @return string
 */
function createCRC32($str)
{
    return sprintf("%u", crc32($str));
}



/**
 * 隐藏手机号码带星
 *
 * @param $mobile
 *
 * @return mixed
 */
function mobileWithStar($mobile)
{
    return preg_replace('/(\d{3})\d{4}/', "$1****", $mobile);
}


/**
 * 时间戳转毫秒
 */
function nowMillisecond()
{
    $time = explode(" ", microtime());
    $time = $time [1] . ($time [0] * 1000);
    $time2 = explode(".", $time);

    return $time2 [0];
}


/**
 * 生成规则 类型+时间戳+时间微妙+3位随机数
 *
 * @param $type
 *
 * @return string
 */
function createNo($type)
{
    $ma = explode(' ', microtime());
    $mi = substr($ma[1], 2, 10) . substr($ma[0], 2, 6);
    $mt = mt_rand(100, 999);

    return $type . $mi . $mt;
}


function trimAll($str)//删除空格
{
    $qian = [" ", "　", "\t", "\n", "\r"];
    $hou = ["", "", "", "", ""];

    return str_replace($qian, $hou, $str);
}

